//
//  CountScoreView.m
//  NewGame
//
//  Created by wwj on 15/10/22.
//  Copyright © 2015年 wwj. All rights reserved.
//

#import "CountScoreView.h"
#import "StageModel.h"
#import "StageRecordModel.h"
#import "SoundTool.h"

#define kBtnShowRatio 88/103.0
#define kBtnWidth 60
#define KBtnHeight 69

#define kBtnTag 10

@interface CountScoreView ()

@property (weak ,nonatomic) IBOutlet UIImageView *socreHint;
@property (weak ,nonatomic) IBOutlet UIImageView *scoreBorder;
@property (nonatomic, weak) IBOutlet UILabel *scoreLabel;
@property (nonatomic, weak) IBOutlet UILabel *unitLabel;

@property (assign, nonatomic) CGFloat endX;
@end
@implementation CountScoreView

-(void)awakeFromNib{
    CGFloat btnW = kBtnWidth;
    CGFloat btnH = KBtnHeight;
    CGFloat btnY = 60;
    [self insertSubview:_scoreBorder belowSubview:_socreHint];
    for (int index = 0; index < 7; index++) {
        CGFloat btnX = index * btnW * kBtnShowRatio;
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.tag = index +kBtnTag;
        button.frame = CGRectMake(btnX, btnY, btnW, btnH);
        [button setBackgroundImage:[UIImage imageNamed:@"other_score"] forState:UIControlStateNormal];
        unichar ch = [@"SABCDEF" characterAtIndex:index];
        [button setTitle:[NSString stringWithFormat:@"%c",ch] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont systemFontOfSize:30];
        button.contentEdgeInsets = UIEdgeInsetsMake(0, 0, 6, 10);
        button.userInteractionEnabled = NO;
        [self addSubview:button];
        [self insertSubview:button belowSubview:_scoreBorder];
    }
    
    self.transform = CGAffineTransformMakeRotation(M_PI_4 / 10);
}

-(void)countScoreWithNewScore:(double)newScore model:(StageModel *)model{
    
    _scoreLabel.text = [NSString stringWithFormat:@"%.f",newScore];
    _unitLabel.text = model.unit;
    
    //计算总的宽度（F的X值）
    double fX = [self viewWithTag:kBtnTag + 6].frame.origin.x;

    //每个格子占据多少分
    double scorePreBtn = (model.max - model.min) / 5;
    
    //每分占的宽度
    double widthPreScore = kBtnWidth * kBtnShowRatio / scorePreBtn;
    
    //计算当前分数占的宽度
    double detail = (newScore - model.min) * widthPreScore;
    
    _endX = fX - detail -1;
    
    //检测是否越界
    if (_endX < 0) {
        _endX = 0;
    }else if (_endX > fX){
        _endX = fX;
    }
    
    
    [[CADisplayLink displayLinkWithTarget:self selector:@selector(update:)] addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
    
}

- (void)update:(CADisplayLink *)time{
    
    static int index = 6;
    
    //修改游标的位置
    CGPoint center = _socreHint.center;
    center.x -= 3;
    if (center.x < _endX) {
        center.x = _endX;
        [time invalidate];
        if (_showResultBlock) {
            _showResultBlock([NSString stringWithFormat:@"%c",[@"sabcdef" characterAtIndex:index]]);
        }
    }
    _socreHint.center = center;
    
    index = center.x / (kBtnWidth * kBtnShowRatio);
    if (index <= 6) {
        UIButton *btn = (UIButton *)[self viewWithTag:index + kBtnTag];
        //只设置一次图片
        if (btn && btn.tag != 100) {
            [btn setBackgroundImage:[UIImage imageNamed:@"cureent_score"] forState:UIControlStateNormal];
            [btn setTag:100];
            _scoreBorder.center = CGPointMake(btn.center.x - 5, btn.center.y - 2);
            [[SoundTool sharedSoundTool] playSound:kSoundGrade(index)];
            if (_rankBlock) {
                _rankBlock([NSString stringWithFormat:@"%c",[@"sabcdef" characterAtIndex:index]]);
            }
        }
    }
}

@end
